import Vue from 'vue'
import Router from 'vue-router'
// import templateFile from '@/components/templateFile' // 模板页
// import test from '@/views/test'
import {Message} from 'element-ui';

import login from '@/views/login/loginLogic' // 登录
import frame from '@/views/frame/frameView'
import frameWork from '@/views/frameWork'

import homeView from '@/views/home/homeView' // 主页
import appManageLogic from '@/views/appManage/appManageLogic' //应用管理
import dictionaryManageLogic from '@/views/dictionaryManage/dictionaryManageLogic' // 字典管理
import menuManageLogic from '@/views/menuManage/menuManageLogic' // 菜单管理
import operateManageLogic from '@/views/operateManage/operateManageLogic' // 运营管理
import organizeManageLogic from '@/views/organizeManage/organizeManageLogic' // 组织管理
import roleManageLogic from '@/views/roleManage/roleManageLogic' // 角色管理
import rolePrivAlloceLogic from '@/views/rolePrivAlloce/rolePrivAlloceLogic' // 角色权限
import userManageLogic from '@/views/userManage/userManageLogic' // 用户权限
import fileManageLogic from '@/views/fileManage/fileManageLogic'; // 文件管理
import fileSearchLogic from '@/views/fileSearch/fileSearchLogic'; // 文件查找

import notFound from '@/views/404.vue';


let session = window.sessionStorage;


Vue.use(Router)

const frameChildren = [
  {
    name: 'homeView',
    path: 'home',
    component: homeView,
  },
  {
    name: 'appManageLogic',
    path: 'appManage',
    component: appManageLogic,
  },
  {
    name: 'dictionaryManageLogic',
    path: 'dictionaryManage',
    component: dictionaryManageLogic,
  },
  {
    name: 'menuManageLogic',
    path: 'menuManage',
    component: menuManageLogic,
  },
  {
    name: 'operateManageLogic',
    path: 'operateManage',
    component: operateManageLogic,
  },
  {
    name: 'organizeManageLogic',
    path: 'organizeManage',
    component: organizeManageLogic,
  },
  {
    name: 'roleManageLogic',
    path: 'roleManage',
    component: roleManageLogic,
  },
  {
    name: 'rolePrivAlloceLogic',
    path: 'rolePrivAlloce',
    component: rolePrivAlloceLogic,
  },
  {
    name: 'userManageLogic',
    path: 'userManage',
    component: userManageLogic,
  },
  {
    name: 'fileManageLogic',
    path: 'fileManage',
    component: fileManageLogic,
  },
  {
    name: 'fileSearchLogic',
    path: 'fileSearch',
    component: fileSearchLogic,
  }
]


const router = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/',
      name: 'login',
      component: login,
    },
    {
      path: '/frame',
      component: frame,
      redirect: '/frame/home',
      meta: {
        requiresAuth: true// 说明需要鉴权
      },
      children: frameChildren
    },
    {
      path: '/frameWork',
      component: frameWork,
      redirect: '/frameWork/home',
      meta: {
        requiresAuth: true// 说明需要鉴权
      },
      children: frameChildren
    },
    {path: '*', name: 'noFound', component: notFound}
  ]
})


// 全局守卫  https://router.vuejs.org/zh-cn/advanced/navigation-guards.html
router.beforeEach((to, from, next) => {
  // 获得那些路由需要token验证
  // console.log("to", to);
  // console.log("from", from);
  // console.log("next", next);
  let userData = JSON.parse(session.getItem('userData'));
  if (to.path == "/" && userData) {
    next('/frame/home');
  } else if (to.matched.some(route => route.meta.requiresAuth)) {
    if (userData) {
      next();
    } else {
      // 没有登录的提示
      Message.error('您还没有登录');
      next('/');
    }
  } else {
    next(); // 确保一定要调用
  }
  // next();
});

export default router;
